﻿using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ETA.Service
{
    public class FlowStepService : IFlowStepService
    {
        #region 属性

        private IFlowStepRepository _IFlowStepRepository { set; get; }


        #endregion

        #region 构造函数

        public FlowStepService(IFlowStepRepository IFlowStepRepository)
        {
            _IFlowStepRepository = IFlowStepRepository;
        }

        #endregion

        public QueryFlowStepResponse QueryFlowStepList(QueryFlowStepRequest req)
        {
            var response = new QueryFlowStepResponse();
            try
            {
                var express = QueryFlowStepExpress(req);
                response.FlowSteps = _IFlowStepRepository.GetAllList(express);
                response.Result.IsSuccess = true;
            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }

        private Expression<Func<FlowStep, bool>> QueryFlowStepExpress(QueryFlowStepRequest request)
        {
            Expression<Func<FlowStep, bool>> express = step =>step.IsDelete==false;
            if (request.FlowStep != null)
            {
                if (!string.IsNullOrEmpty(request.FlowStep.FlowStepId))
                {
                    express = flowStep => flowStep.FlowStepId == request.FlowStep.FlowStepId && request.FlowStep.IsDelete == false;
                }
                else
                    if (!string.IsNullOrEmpty(request.FlowStep.FlowStepName))
                    {
                        express = flowStep => flowStep.FlowStepName == request.FlowStep.FlowStepName && request.FlowStep.IsDelete == false;
                    }
                    else
                        if (request.FlowStep.FlowIndex != null)
                        {
                            express = flowStep => flowStep.FlowIndex == request.FlowStep.FlowIndex && request.FlowStep.IsDelete == false;
                        }
            }
            return express;
        }

    }
}
